/******************************************************************************************************
Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.

This file is part of the Mnova scripting toolkit.

Authorized users of Mnova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/*globals Application, Settings, samePhaseAsCurrent, MnUi*/
/*jslint plusplus: true, indent: 4*/

// Automatically corrects the phase of the current spectrum in a stack, 
// then applies the calculated phase to the rest of the spectra in a stack
function autophaseCurrentSamePhaseRest() {
	"use strict";
	var i, proc, setMethods, d,	strMethods,
		set = new Settings("NMR/Phase Correction"),
		spec = Application.nmr.activeSpectrum();
	if (!spec.isValid()) {
		return;
	}

	proc = spec.proc;
	if (spec.dimCount === 1) {
		setMethods = set.value("Automatic 1D", ["0"]);
		strMethods = "";
		for (i = 0; i <= setMethods.length; i++) {
			switch (setMethods[i]) {
			case '0': strMethods += "Global "; break;
			case '1': strMethods += "Selective "; break;
			case '2': strMethods += "Metabonomics "; break;
			case '3': strMethods += "Whitening "; break;
			}
		}
		proc.setParameter("PC.Method", strMethods);
	} else {
		for (d = 1; d <= 2; d++) {
			setMethods = set.value("Automatic 2D f{0}".format(d), ["0", "1"]);
			strMethods = "";
			for (i = 0; i <= setMethods.length; i++) {
				switch (setMethods[i]) {
				case '0': strMethods += "Projections "; break;
				case '1': strMethods += "Whitening2D "; break;
				}
			}
			proc.setParameter("PC[{0}].Method".format(d), strMethods);
		}
	}
	spec.processCurSpec();
	samePhaseAsCurrent(spec);
}

if (this.MnUi && MnUi.scripts_nmr) {
	MnUi.scripts_nmr.scripts_nmr_AutoPhaseCurrentSamePhaseRest = autophaseCurrentSamePhaseRest;
}
